<?php
namespace Addons\AutoComplete\Controller;
class IndexController extends \Addon
{
    public function __construct()
    {
        parent::__construct();
        $this->model = BoxModel('addon_auto_complete');
        $this->table_name = I('table_name');
        $this->auto_complete_name = I('auto_complete_name');
        //dump($this->auto_complete_name);exit();
        $this->memuNav = $this->auto_complete_name;//选中菜单
        if ($this->auto_complete_name) {
            $this->table_data = $this->model->where(array('name' => $this->auto_complete_name))->find();
            $this->table_name = $this->table_data['table_name'];
            $this->memuNav = $this->table_data['name'];
        }
        if ($this->table_name) {
            $this->table_model = BoxModel($this->table_name);
        }

        $this->params = $this->_params();

    }

    /**
     * 自动完成模型
     * @return
     */

    public function index()
    {
        $data = $this->model->get("", 10);
        foreach ($data['list'] as $k => $one) {
            $one['param'] = array(
                'auto_complete_name' => $one['name'],
            );
            hook("table_" . $one['table_name'] . "_get", $one);
            hook("table_" . $one['name'] . "_get", $one);
            $data['list'][$k] = $one;
        }
        $this->data = $data['list'];
        $this->show_page($data['pager']);
        $this->admin_display();
    }


    /**
     * 添加
     */
    public function add()
    {
        $id = I('id');
        if (IS_POST) {
            //	dump(I());

            $row = $this->model->create();
            if (!$row['form_name']) {
                $row['form_name'] = "table_" . $row['name'];
            }
            if (!$row['name']) {
                $row['name'] = $row['table_name'];
            }
            $row['_showBtnHtml'] = stripslashes(htmlspecialchars_decode(I('_showBtnHtml')));
            if ($id) {
                $this->model->where(array('id' => $id))->save($row);
            } else {
                $id = $this->model->add($row);
                if ($id) {
                    $this->saveField();
                }
            }
            $this->jump("保存成功", addons_url('add', array('id' => $id)));
        }
        if ($id) {
            $this->data = $this->model->find($id);
        }
        $this->admin_display();
    }


    function saveField()
    {
        $fields = $this->table_model->getDbFields();
        $model = BoxModel('addon_fields');
        foreach ($fields as $one) {
            $row = array(
                'group_name' => 'table_' . $this->table_name,
                'fieldName' => $one,
                'fieldDisplay' => $one,
                'displayType' => 1,
                'IsDisplay' => 1,
                'valueType' => 1,
            );
            $model->add($row);
        }

    }

    /**
     * 删除
     * @return [type] [description]
     */
    public function del()
    {
        if ($id = I('id')) {
            $this->model->delete($id);
            $this->jump("删除成功");
        }
    }

    //列表查询条件
    public function buildWhere($_where)
    {
        $_where = stripslashes(htmlspecialchars_decode($_where));
        $_get = $_GET;
        $_where = explode("\r\n", $_where);
        $op = array();
        foreach ($_where as $k => $one) {
            $p = explode(":", $one);
            $op[trim($p[0])] = trim($p[1]);
        }
        $where = "";
        foreach ($op as $k => $one) {
            if ($_get[$k]) {
                $where .= " (" . str_replace("$" . $k, $_get[$k], $one) . ")";
            }
        }
        return $where;
    }

    //全局参数
    public function _params($table_params = NULL, $data = NULL)
    {
        $params = array();
        $table_params = $table_params ? $table_params : $this->table_data['params'];
        if (!$table_params) {
            $this->params = array();
            return;
        }
        $_get = $data ? $data : $_GET;
        $p = explode(",", $table_params);

        foreach ($_get as $k => $one) {
            if (in_array($k, $p)) $params[$k] = $one;
        }
        return $params;
    }

    /**
     * list_table
     * @return [type] [description]
     */
    public function list_table()
    {
        //dump("123");exit();
        $where = 1;
        $limit = $this->table_data['_limit'] ? $this->table_data['_limit'] : "10";
        $order_by = $this->table_data['_order_by'];
        if ($this->table_data['_where']) {
            $add_where = $this->buildWhere($this->table_data['_where']);
            if ($add_where) {
                $where .= " and (" . $this->buildWhere($this->table_data['_where']) . ")";
            }
        }
       
        $this->show_fields = BoxModel("addon_data")->get(array('group_name' => "table_" . $this->auto_complete_name), "", "order_by");
        $data = $this->table_model->get($where, $limit, $order_by);
							//	 dump($this->table_model);
        $rows = $data['list'];
        foreach ($rows as $k => $one) {
            foreach ($this->show_fields as $one1) {
                $tpl = $one1['tpl'];
                if ($tpl) {
                    $_get = (array)$_GET;
                    $fetchData = array_merge($one, $_get);
                    //dump($fetchData);
                    $rows[$k][$one1['field']] = $this->fetchHtml($fetchData, stripslashes(htmlspecialchars_decode($tpl)), "vo");
                }
            }
        }
        $this->list = $rows;
        $this->show_page($data['pager']);
        $this->admin_display();
    }

    public function add_table()
    {
        $pk = $this->table_model->getPk();
        if ($pk) $id = I($pk);
        if (IS_POST) {
            $row = $this->table_model->create();
            hook("before_" . $this->table_data['table_name'] . "_add", $row);
            hook($this->table_data['hook_before_add'], $row);
            if ($id) {
                $r = $this->table_model->save($row);
                $row['act'] = "up";
            } else {
                $id = $this->table_model->add($row);
                $row[$pk] = $id;
                $row['act'] = "add";
            }
            hook("after_" . $this->table_data['table_name'] . "_add", $row);
            hook($this->table_data['hook_after_add'], $row);
            $this->jump("保存成功", addons_url('list_table', $this->params));
        }

        if ($id) {
            $this->data = $this->table_model->load($id);
        }
        $this->admin_display();

    }


    public function del_table()
    {
        $id = I("id");
        if ($id) {
            hook($this->table_data['hook_before_del']);
            $r = $this->table_model->delete($id);
            if ($r) {
                hook($this->table_data['hook_after_del']);
            }
        }
        $this->jump("删除成功");
    }

}